package test04.demo01;

import java.util.concurrent.TimeUnit;

public class MyThread extends Thread {
    public MyThread() {
    }

    public MyThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("采集数据");
            boolean flag = Thread.currentThread().isInterrupted();
            if (flag) {
                System.out.println("采集结束，做一些收尾工作");
                return;
            }
            try {
                // 每隔两秒采集
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                // 二次中断，把清除的中断标记重新设置为true
                Thread.currentThread().interrupt();
            }
        }
    }
}
